﻿using System.Xml;

namespace KPIS.GERP.Models.Class
{
	/// <summary>
	/// Basic load and read attribute int XML file
	/// </summary>
	public static class XmlUtility
	{
		/// <summary>
		/// Method to load xml file as object XmlDocument
		/// </summary>
		/// <param name="xmlPath">Xml file path</param>
		/// <returns>XmlDocument loaded</returns>
		public static XmlDocument LoadXml(string xmlPath)
		{
			XmlDocument xmlDocumentTemp = new XmlDocument();
			XmlTextReader reader = new XmlTextReader(xmlPath);
			reader.WhitespaceHandling = WhitespaceHandling.None;
			xmlDocumentTemp.Load(reader);
			reader.Close(); 
			return xmlDocumentTemp;
		}

		/// <summary>
		/// Load attribute value from xmlNode in param
		/// </summary>
		/// <param name="xmlNode">Xml Node object to fetch value</param>
		/// <param name="attributeName">Attribute name of the node</param>
		/// <returns>value as string</returns>
		public static string FetchAttributeValue(XmlNode xmlNode, string attributeName)
		{
			if (xmlNode == null || xmlNode.Attributes == null || xmlNode.Attributes[attributeName] == null) return null;

			return xmlNode.Attributes[attributeName].Value;
		}
	}
}
